<?php

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

    header("Location: login.php");
}
//
include 'i_functions.php';
//
//==========================================
//	CONNECT TO THE LOCAL DATABASE
//==========================================
$user_name = "root";
$pass_word = "haddons";
$database = "PPI";
$server = "127.0.0.1";
$csvFile = "";
//
include 'i_head.php';
//
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
//
if ($db_found) {
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        $csvFile = $_GET['csvFile'];
    }
    if ($csvFile == "") {
        $csvFile = "data/Cases.csv";
    }
    print $csvFile . ".....<p>";
    $file_handle = fopen($csvFile, "r");
    while (!feof($file_handle)) {
        $line = fgets($file_handle);
        $csvCols = explode(",", $line);
//
        $pcode = $csvCols[15];
        $sname = $csvCols[6];

        $pcode = htmlspecialchars($pcode);
        $sname = htmlspecialchars($sname);
//
        if ($pcode) {
            $q_pcode = quote_smart($pcode, $db_handle);
            $q_sname = quote_smart($sname, $db_handle);
            $SQL = "SELECT * FROM client WHERE Postcode = $q_pcode";
            if ($sname) {
                $SQL = $SQL . " AND Surname = $q_sname";
            }
            print "<br>" . $SQL;
            $result = mysql_query($SQL);
            $num_rows = mysql_num_rows($result);
            //====================================================
            //	CHECK TO SEE IF THE $result VARIABLE IS TRUE
            //====================================================
            if ($result) {
                //include 'i_head.php';
                if ($num_rows > 0) {
                    $message = " - client already exists:";
                    print $message;
                    while ($db_field = mysql_fetch_assoc($result)) {
                        $pline = "<br> - <A href=\"./Client.php?client_ID=" . $db_field['ID'] . "\">" . $db_field['ID'] . "</A> - ";
                        $pline = $pline . $db_field['FirstNames'] . " " . $db_field['Surname'] . ", ";
                        $pline = $pline . $db_field['House'] . " " . $db_field['Street'];
                        if ($db_field['Town']) {
                            $pline = $pline . ", " . $db_field['Town'];
                        }
                        if ($db_field['County']) {
                            $pline = $pline . ", " . $db_field['County'];
                        }
                        $pline = $pline . ", ";
                        $pline = $pline . ", " . $db_field['Postcode'] . "";
                        print $pline;
                    }
                } else {
                    $message = " - new client";
                    print $message;
                    //session_start();
                    //$_SESSION['login'] = "";
                    //header("Location: signup.php");
                }
            } else {
                $errorMessage = "Error logging on";
            }
        }
    }
    mysql_close($db_handle);
    include 'i_foot.html';
}
?>